// pages/classify/classify.js
var app = getApp();
var com = require('../../common.js');
Page({
  data: {
    active: 0,
    statusd: 1,
    actives: 0,
    nextUrl: '',
    nextUrlCastamer: '',
    leftList: [],
    infoList: [],
    couponsValue: '',
    itemsValue: '',
    arlteView: false,
    num: 0,
    nums: 0,
    searchList: [{
      search: '',
      max: 0,
      min: 0,
      sale: '',
      category: ''
    }, {
      search: '',
      max: 0,
      min: 0,
      sale: '',
      category: ''
    }],
  },
  onLoad: function(options) {
    var _this = this
    setTimeout(() => {
      this.setData({
        statusd: 0
      })
    }, 800)
    if (wx.getStorageSync('token')) {} else {
      wx.reLaunch({
        url: '/pages/login/login',
      })
      return false;
    }
    _this.coupons(0);
    _this.canstomer()
  },
  s() {},
  switchTap(e) {
    console.log(e)
    if (e.currentTarget.dataset.id == 0) {
      var l = [],
        _this = this
      l.push(e.currentTarget.id)
      com.link(app.link.coupon_sale, {
        sale: e.detail.value.length,
        items: l
      }, function(res) {
        console.log(res)
        if (res.data.state) {
          wx.showToast({
            title: '修改成功'
          })
        }
      })
    } else {
      com.link(app.link.couponSt + e.currentTarget.id + '.st', {
        sale: e.detail.value.length,
      }, function(res) {
        console.log(res);
        wx.showToast({
          title: res.data.message,
        })
      })
    }

  },
  //回车键筛选
  searchConFirm(e) {
    console.log(e)
    var _this = this,
      value = e.detail.value,
      type = _this.data.active,
      data = _this.data.searchList
    if (type == 0) {
      data[0].search = value
      com.link(app.link.coupons, {
        search: value
      }, function(res) {
        console.log(res)
        _this.setData({
          info: res.data.data,
          nextUrl: res.data.links.next,
          searchList: data
        })
      }, "GET")
    } else {
      data[1].search = value
      com.link(app.link.items, {
        search: value
      }, function(res) {
        console.log(res)
        var list = res.data.data
        // for (let i in res.data.data){
        //   list.push(res.data.data[i])
        // }
        _this.setData({
          infoList: list,
          nextUrlCastamer: res.data.links.next,
          searchList: data
        })
      }, "GET")
    }
    console.log(value)
  },
  setActive: function(e) {
    var _this = this
    this.setData({
      active: e.currentTarget.dataset.index,
      actives: ''
    })
    if (e.currentTarget.dataset.index == 1) {
      _this.coupons(e.currentTarget.dataset.index);
    } else {
      _this.coupons(e.currentTarget.dataset.index);
    }
  },
  onReady: function() {},
  //获取套餐券列表
  coupons(e) {
    var _this = this
    if (e == 0) {
      var data = _this.data.searchList
      data[0].category = _this.data.actives
      com.link(app.link.coupons, data[0], function(res) {
        console.log(res)
        _this.setData({
          info: res.data.data,
          nextUrl: res.data.links.next
        })
      }, "GET")
    } else {
      var data = _this.data.searchList
      com.link(app.link.items, data[1], function(res) {
        console.log(res)
        var list = res.data.data
        // for (let i in res.data.data){
        //   list.push(res.data.data[i])
        // }
        _this.setData({
          infoList: list,
          nextUrlCastamer: res.data.links.next
        })
      }, "GET")
    }
  },
  onShow: function() {
    var _this = this
    if (wx.getStorageSync('token')) {} else {
      return false;
    }
    _this.coupons(_this.data.active);
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      })
    }
  },
  onHide: function() {},
  onUnload: function() {},
  onPullDownRefresh: function() {},
  onReachBottom: function() {
    console.log('1')
  },
  lower(e) {
    var id = e.currentTarget.id
    var _this = this
    if (id == 1) {
      if (!_this.data.nextUrl) {
        return false;
      }
      com.link(_this.data.nextUrl, {}, function(res) {
        console.log(res)
        _this.setData({
          info: res.data.data,
          nextUrl: res.data.links.next
        })
      }, "GET")
    } else {
      if (!_this.data.nextUrlCastamer) {
        return false;
      }
      com.link(_this.data.nextUrlCastamer, {}, function(res) {
        console.log(res)
        var list = _this.data.infoList
        for (let i in res.data.data) {
          list.push(res.data.data[i])
        }
        _this.setData({
          infoList: list,
          nextUrlCastamer: res.data.links.next
        })
      }, "GET")
    }
  },
  clickList() {
    var _this = this,
      next = _this.data.nextUrl
    if (!next) {
      wx.showToast({
        title: '没有更多了',
        icon: 'none'
      })
    } else {
      com.link(next, {}, function(rep) {
        console.log(rep)
      })
    }
  },
  onShareAppMessage: function() {},
  clicktap(e) {
    console.log(e)
    var _this = this,
      type = e.currentTarget.dataset.id,
      data = _this.data.searchList
    _this.setData({
      actives: e.currentTarget.id
    })
    if (type == 1) {
      data[0].category = e.currentTarget.id
      com.link(app.link.coupons, {
        category: e.currentTarget.id
      }, function(res) {
        console.log(res)
        if (res.statusCode == 429) {
          wx.showToast({
            title: '请勿频繁筛选',
            icon: 'none'
          })
          return false;
        }
        var list = res.data.data
        // for (let i in res.data.data){
        //   list.push(res.data.data[i])
        // }
        _this.setData({
          info: list,
          nextUrl: res.data.links.next,
          searchList: data
        })
      }, "GET")
    } else {
      data[1].category = e.currentTarget.id
      com.link(app.link.items, {
        category: e.currentTarget.id
      }, function(res) {
        console.log(res)
        if (res.statusCode == 429) {
          wx.showToast({
            title: '请勿频繁筛选',
            icon: 'none'
          })
          return false;
        }
        var list = res.data.data
        // for (let i in res.data.data){
        //   list.push(res.data.data[i])
        // }
        _this.setData({
          infoList: list,
          nextUrlCastamer: res.data.links.next,
          searchList: data
        })
      }, "GET")
    }

  },
  //获取列表分类
  canstomer() {
    var _this = this
    com.link(app.link.categorys, {}, function(res) {
      console.log(res)
      _this.setData({
        rightList: res.data.data,
        actives: '',
      })
    }, "GET")

    com.link(app.link.categorySearch, {
      item: 1
    }, function(res) {
      console.log(res)
      _this.setData({
        leftList: res.data.data,
        actives: '',
      })
    }, "GET")
  },
  showView() {
    var _this = this
    _this.setData({
      arlteView: _this.data.arlteView ? false : true
    })
  },
  sliderValue(e) {
    var _this = this,
      data = _this.data.searchList
    data[_this.data.active].max = e.detail.value
    console.log(e)
    _this.setData({
      searchList: data
    })
  },
  sliderValues(e) {
    var _this = this,
      data = _this.data.searchList
    data[_this.data.active].min = e.detail.value
    console.log(e)
    _this.setData({
      searchList: data
    })
  },
  //重置搜索框
  onReltal(e) {
    var _this = this,
      data = _this.data.searchList,
      i = _this.data.active
    data[i].search = ''
    data[i].max = 0
    data[i].min = 0
    data[i].sale = ''
    _this.setData({
      searchList: data
    })
  },
  //获取搜索商品名称
  onInputtap(e) {
    var _this = this,
      data = _this.data.searchList
    data[_this.data.active].search = e.detail.value
    _this.setData({
      searchList: data
    })
  },
  //筛选上下架按钮
  saleUpdown(e) {
    var _this = this,
      id = e.currentTarget.id,
      data = _this.data.searchList,
      i = _this.data.active
    data[i].sale = id == 0 ? 'up' : 'down'
    _this.setData({
      searchList: data
    })
  },
  onSearchBox() {
    var _this = this,
      i = _this.data.active,
      data = _this.data.searchList
    if (i == 0) {
      com.link(app.link.coupons, data[0], function(res) {
        var list = res.data.data
        _this.setData({
          info: list,
          nextUrl: res.data.links.next,
          arlteView: false
        })
      }, "GET")
    } else {
      com.link(app.link.items, data[1], function(res) {
        console.log(res)
        var list = res.data.data
        _this.setData({
          infoList: list,
          nextUrlCastamer: res.data.links.next,
          arlteView: false
        })
      }, "GET")
    }
  }
})